package com.hsy.baoinbao.modules.my.view;

import android.content.Context;
import android.content.Intent;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.deparse.dpframe.ui.activity.DPBaseActivity;
import com.hsy.baoinbao.R;
import com.hsy.baoinbao.application.BIBApplication;
import com.hsy.baoinbao.modules.login.LoginActivity;
import com.hsy.baoinbao.modules.main.ui.activity.MainActivity;
import com.hsy.baoinbao.utils.CommonUtils;

/**
 * Created by Administrator on 2016/3/23.
 */
public class CommonPersonRelativeLayout extends RelativeLayout implements View.OnClickListener {
    private String leftText;
    private int leftTextColor = Color.GRAY;
    private float leftTextSize;
    private int rightSrc;

    private Class<?> mTarget;
    private Context mContext;
    private String[][] data;

    private TextView leftTextView;
    private ImageView rightImageView;

    private LayoutParams leftParams;
    private LayoutParams rightParams;

    private boolean isDo = false;
    private Context intentContext;

    public CommonPersonRelativeLayout(Context context) {
        super(context);
        init(context);
    }

    public CommonPersonRelativeLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CommonPersonRelativeLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CommonPersonRelativeLayout);
        leftText = typedArray.getString(R.styleable.CommonPersonRelativeLayout_leftText);
        leftTextColor = typedArray.getColor(R.styleable.CommonPersonRelativeLayout_leftTextColor, leftTextColor);
        leftTextSize = typedArray.getDimension(R.styleable.CommonPersonRelativeLayout_leftTextSize, leftTextSize);
        rightSrc = typedArray.getResourceId(R.styleable.CommonPersonRelativeLayout_rightSrc, R.mipmap.right_arrow_light_gray);
        typedArray.recycle();
        init(context);
    }

    private void init(Context context) {
        mContext = context;
        setOnClickListener(this);

        leftTextView = new TextView(context);
        rightImageView = new ImageView(context);
        leftTextView.setText(leftText);
        leftTextView.setTextColor(leftTextColor);
        leftTextView.setTextSize(leftTextSize);
        leftTextView.setGravity(Gravity.CENTER);
        leftTextView.setPadding(20, 0, 0, 0);
        rightImageView.setImageResource(rightSrc);


        leftParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
        leftParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, TRUE);
        addView(leftTextView, leftParams);

        rightParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
        rightParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, TRUE);
        addView(rightImageView, rightParams);
    }

    public void setTarget(Class<?> cls, String[]... data) {
        this.mTarget = cls;
        this.data = data;
    }

    public void doThingIntent(boolean isDo, Context context) {
        if (isDo) {
            CommonUtils.clearToken(context);
            CommonUtils.clearUid(context);
            BIBApplication.getInstance().unregisterXGPushService();
            ((MainActivity) context).startActivityWithTaskCleared(LoginActivity.class);
        } else {
// do nothing
        }
    }

    public void setDo(boolean is, Context context) {
        this.isDo = is;
        this.intentContext = context;
    }

    @Override
    public void onClick(View view) {
        doThingIntent(isDo, intentContext);
        Intent intent = null;
        if (null != mTarget) {
            intent = new Intent(mContext, mTarget);
            mContext.startActivity(intent);
        }

    }
}
